
var __UIPve_instance = null;
function getUIPve() { return __UIPve_instance; }

var UIPve = function(){};

UIPve.prototype.onDidLoadFromCCB = function()
{   
    cc.log("UIPve.onDidLoadFromCCB");
    __UIPve_instance = this;
    
    // 初始化
    this._curItemIdx = -1;
    this._curMissionid = -1;
    this._items = [];
    this._data = null;
    
	this._layerFB = cc.Layer.create();
	this._layerFB.setAnchorPoint(cc.p(0,0));
	this._layerFB.setPosition(cc.p(0,0));
	this.uiTouchLayer.addChild(this._layerFB); // 加入到设计好的界面中
    
    // 描述面板(需要在menu之前创建, 才能显示在下面)
    this._descText = null;
    this._descCost = null;
    this._descAward = null;
    
    this._descPanel = this.createDesc();
    this._layerFB.addChild(this._descPanel);
    
    // 创建menu
    this._menuFB = cc.Menu.create();
    this._menuFB.setAnchorPoint(cc.p(0,0));
    this._menuFB.setPosition(cc.p(0,0));
    this._layerFB.addChild(this._menuFB);
    
	// test
	//this.setData([[0,1],[1,1],[2,1]]);
    this.showDesc(false);
};


// 更新界面
UIPve.prototype.updateData = function()
{
    md = DataManager.playerMissionData;
    
    var maxId = -1;
    for(var missionId in md) {
        if(missionId > maxId) {
            maxId = missionId;
        }
    }
    
    maxId = parseInt(maxId);
    //cc.log('maxId:'+maxId);
    //cc.log(typeof(maxId));
    
    if(maxId == -1) {
        md[1] = false;  // 没有数据的特殊处理
    }
    else {
        // 显示下一个关卡
        if(DATA_mission[maxId+1]) {
            //* md 是引用, 会改变playerMissionData中的数据, 有点小问题
            if(md[maxId] == true) { md[maxId+1] = false; }
        }
    }
    
    // 做keys的排序
    var keys = [];
    for(var id in md) {
        keys.push(id);
    }
    
    keys.sort();
    d = [];
    for each(var id in keys) {
        d.push([id, md[id]]);
    }
    
    this.setData(d);
}


// 设置数据 格式:[[关卡id,状态(false:未完成,true:已完成)], ..]
UIPve.prototype.setData = function(data)
{
    cc.log("UIPve.setData " + data);
    this._data = data;  // 记录数据
    
    for(var i in data) {
        var dt = data[i];
        id = dt[0];
        st = dt[1];

        //cc.log(typeof(id));
        //cc.log(id);
        //cc.log(st);
        var name = DATA_mission[id].name + (st ? '√':'');
        var item = null;
        if(i > this._items.length-1) {
            // 创建新item
            item = this.createItem(this._menuFB, i, name, false);
            this._items.push(item);
        }
        
        // 更新可见状态
        if(item) {
            item.setVisible(true);
        } else {
            cc.log('一般不会走到这里, 出错了');
        }
    }
    
    this.updatePosition();
    
};

UIPve.prototype.updatePosition = function()
{
	var x = 0, y = 86;
	for (var i in this._items) {
		item = this._items[i];
		
		x = i * 40 + 5;
		if(this._curItemIdx > -1 && i > this._curItemIdx) { x += 197; }
		item.setPosition(x, y);
        
        // descPanel
        if(this._curItemIdx == i) {
            this._descPanel.setPositionX(x + 33);
        }
	}
};

UIPve.prototype.showDesc = function(isShow, menuItemIndex, id)
{
    this._descPanel.setVisible(isShow);

    if(isShow) {
        this._curItemIdx = menuItemIndex;
        this._curMissionid = id;
        this.setDesc(id);
    } else {
        this._curItemIdx = -1;
        this._curMissionid = -1;
    }
    
    this.updatePosition();
};


// 选中某项
UIPve.prototype.onSelect = function(target)
{
    var index = target.getTag();
    var id = this._data[index][0];
    cc.log('onSelect index:'+index + '| id:'+id);
    
    if(this._curItemIdx == index) {
        this.showDesc(false, 0, 0);
    } else {
        this.showDesc(true, index, id);
    }
}


UIPve.prototype.createItem = function(parent, tag, name, isChapter)
{   
    cc.log("createItem " + name);
    
    //node = cc.Node.create();
    //bg = cc.Sprite.createWithSpriteFrameName("UIPve/frame_3.png");
    //bg.setAnchorPoint(cc.p(0,0));
    //node.addChild(bg);
    
    node = cc.MenuItemImage.create("res/Misc/frame_3.png", "res/Misc/frame_3.png", this.onSelect, this);
    node.setAnchorPoint(cc.p(0,0));
    node.setTag(tag);   // 存储tag
    
    // 关卡名字
    t = glb.setUprightString(name);
    font = 'Helvetica';
    txt = cc.LabelTTF.create(t, font, 22);
    txt.setAnchorPoint(cc.p(0,1));
    txt.setPosition(6,145);
    txt.setColor(cc.BLACK);
    node.addChild(txt);
    
    parent.addChild(node);
    return node;
};

// 描述面板
UIPve.prototype.createDesc = function()
{
    cc.log("createDesc ");
    
    node = cc.Node.create();
    node.setPosition(116, 89);
    
    bg = cc.Sprite.createWithSpriteFrameName("UIPve/frame_4.png");
    bg.setAnchorPoint(cc.p(0,0));
    node.addChild(bg);

    // 任务描述
    font = 'Helvetica';
    txt = cc.LabelTTF.create('这是任务描述\n这是任务描述\n这是任务描述\n这是任务描述\n', font, 14);
    txt.setAnchorPoint(cc.p(0,1));
    txt.setPosition(6,143);
    txt.setColor(cc.BLACK);
    node.addChild(txt);
    
    cost = cc.LabelTTF.create('消耗', font, 14);
    cost.setAnchorPoint(cc.p(0,1));
    cost.setPosition(6,53);
    cost.setColor(cc.BLACK);
    node.addChild(cost);
    
    award = cc.LabelTTF.create('奖励', font, 14);
    award.setAnchorPoint(cc.p(0,1));
    award.setPosition(6,33);
    award.setColor(cc.BLACK);
    node.addChild(award);
    
    // 闯关按钮
    menu = cc.Menu.create();
    menu.setAnchorPoint(cc.p(0,0));
    menu.setPosition(cc.p(164,0));
    btn = cc.MenuItemImage.create("res/Misc/btncommand1.png", "res/Misc/btncommand2.png", this.onBtnBattle, this);
    btn.setAnchorPoint(cc.p(0,0));
    btn.setPosition(0,0);
    btn.setScale(0.5);
    menu.addChild(btn);
    node.addChild(menu);
    
    // 闯关文字
    txtBtn = cc.LabelTTF.create('闯', font, 14);
    txtBtn.setPosition(180,16);
    node.addChild(txtBtn);
    
    this._descText = txt;
    this._descCost = cost;
    this._descAward = award;
    return node;
};

// 启动战斗按钮
UIPve.prototype.onBtnBattle = function(target) {
    if (this._curMissionid == -1) return;
    BattleManager.startBattlePve(this._curMissionid);
}

// 参数:关卡ID
UIPve.prototype.setDesc = function(id)
{
    cc.log('setDesc:'+id);
    
    var dt = DATA_mission[id];
    
    this._descText.setString(dt.desc);
    
    this._descCost.setString('消耗体力:'+dt.cost_tili);
    
    this._descAward.setString('掉落道具:'+dt.award_item);
};

